/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
// -*- C++ -*-
//
// $Id$

// ****  Code generated by the The ACE ORB (TAO) IDL Compiler v1.7 ****
// TAO and the TAO IDL Compiler have been developed by:
//       Center for Distributed Object Computing
//       Washington University
//       St. Louis, MO
//       USA
//       http://www.cs.wustl.edu/~schmidt/doc-center.html
// and
//       Distributed Object Computing Laboratory
//       University of California at Irvine
//       Irvine, CA
//       USA
//       http://doc.ece.uci.edu/
// and
//       Institute for Software Integrated Systems
//       Vanderbilt University
//       Nashville, TN
//       USA
//       http://www.isis.vanderbilt.edu/
//
// Information about TAO is available at:
//     http://www.cs.wustl.edu/~schmidt/TAO.html

// TAO_IDL - Generated from 
// .\be\be_codegen.cpp:1203

#include "Session_i.h"
#include "IoSessionManager.h"
#include "config.h"

// Implementation skeleton constructor
Taonet_CallbackClient_i::Taonet_CallbackClient_i (IoEventHandler *handler)
:handler_(handler)
{
}

// Implementation skeleton destructor
Taonet_CallbackClient_i::~Taonet_CallbackClient_i (void)
{
}

void Taonet_CallbackClient_i::process (
    const ::Taonet::ByteSeq & msg)
{
  ACE_CString msg_string ((char *)msg.get_buffer() );
	msg_string[msg.length()] = '\0';

	if (handler_)
		handler_->OnRead (0, IoEventHandler::MESSAGE, msg.get_buffer(), msg.length());
}

// Implementation skeleton constructor
Taonet_Session_i::Taonet_Session_i (IoSessionManager *manager)
: manager_(manager), dispose_(false)
{
}

// Implementation skeleton destructor
Taonet_Session_i::~Taonet_Session_i (void)
{
	ACE_DEBUG ((LM_DEBUG, ACE_TEXT("Taonet_Session_i::~Taonet_Session_i ()")));
}

::CORBA::ULong Taonet_Session_i::login (
    ::Taonet::CallbackClient_ptr cb)
{
	callback_ = ::Taonet::CallbackClient::_duplicate(cb);
	dispose(false);

	return key();
}

void Taonet_Session_i::logout (
    void)
{
   dispose(true);
}

void Taonet_Session_i::process (
    const ::Taonet::ByteSeq & msg)
{
  ACE_CString msg_string((char *)msg.get_buffer() );
	msg_string[msg.length()] = '\0';
	
	manager_->recv (key(), IoEventHandler::MESSAGE, msg.get_buffer(), msg.length());
}

// Implementation skeleton constructor
Taonet_SessionAdmin_i::Taonet_SessionAdmin_i (IoSessionManager *manager)
:manager_(manager)
{
}

// Implementation skeleton destructor
Taonet_SessionAdmin_i::~Taonet_SessionAdmin_i (void)
{
}

::Taonet::Session_ptr Taonet_SessionAdmin_i::create (
    const char * id)
{
	static ACE_UINT32 allocator=0;
  static char buf[64];

	ACE_OS::memset (buf, '\0', sizeof (buf));
	ACE_OS::sprintf (buf, "%s %u",SESSION_OBJ_STRING, ++allocator);
	PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId (buf);
      
	CORBA::Object_var obj = poa_->create_reference_with_id (oid.in (), "IDL:Taonet/Session:1.0");
	Taonet::Session_var s = Taonet::Session::_narrow (obj.in ());

	return s._retn();
}

::CORBA::UShort Taonet_SessionAdmin_i::max_capacity (
    void)
{
  return manager_->max_capacity();
}

::CORBA::UShort Taonet_SessionAdmin_i::current_size (
    void)
{
  return manager_->current_size();
}


